#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int mod = 1e9 + 7;

int gcd(int a, int b)
{
    return b ? gcd(b, a % b) : a;
}

ll qmi(ll a, ll b)
{
    ll res = 1;
    while(b)
    {
        res = res * a % mod;
        b >>= 1;
        a = a * a % mod;
    }
    return res;
}

void solve()
{
    int n, k;
    cin >> n >> k;
    vector<int>a(n);
    for(int i = 0; i < n; i++)
    {
        cin >> a[i];
    }
    sort(a.begin(), a.end());
    int l = a[n - 1], r = a[n - 1] + k - 1;
    for(int i = 0; i < n - 1; i++)
    {
        a[i] = a[i] + ((l - a[i]) / (2 * k)) * (2 * k);
        if(a[i] + k < l)
            a[i] += 2 * k;
        l = max(l, a[i]);
        r = max(r, a[i] + k - 1);
    }
    if(l > r)
        cout << -1 << endl;
    else cout << l << endl;
}

int main()
{
    int t;
    cin >> t;
    while(t--)
        solve();
    return 0;
}